import { defineStore } from "pinia";
import { getUserInfoAPI } from "@/apis/user";
import { getVipInfoAPI } from "@/apis/member";

export const useUserStore = defineStore("user", {
  state: () => ({
    token: "",
    userInfo: null,
    plusInfo: null,
  }),

  actions: {
    setToken(token) {
      this.token = token;
      uni.setStorageSync("token", token);
    },

    setUserInfo(info) {
      this.userInfo = info;
      uni.setStorageSync("userInfo", info);
    },

    getUserInfo() {
      getUserInfoAPI().then((res) => {
        if (res.code === "200") {
          this.setUserInfo(res.data);
        }
      });
    },

    async getPlusInfo() {
      const { code, data } = await getVipInfoAPI();
      console.log("getPlusInfo:", code, data);
      if (code == 200) {
        this.plusInfo = data;
      }
    },

    logout() {
      this.token = "";
      this.userInfo = null;
      uni.removeStorageSync("token");
      uni.removeStorageSync("userInfo");
    },
  },
});
